//
//  AMPWelcomeViewController.swift
//  LivingHelper
//
//  Created by Ampaw on 2017/9/11.
//  Copyright © 2017年 Ampaw. All rights reserved.
//

import UIKit

class AMPWelcomeViewController: UIViewController {
    
    var welcomeView:UIScrollView?

    override func viewDidLoad() {
        super.viewDidLoad()
        
        UIApplication.shared.isStatusBarHidden = true
        setUpUI()
    }
    
    deinit {
        AMPLog.LogClassDestory("AMPWelcomeViewController")
    }
    
    // MARK: - 创建UI视图
    func setUpUI(){
        
        welcomeView = UIScrollView()
        welcomeView?.contentSize = CGSize(width: AMPConstants.AMP_SCREEN_WIDTH * 3, height: AMPConstants.AMP_SCREEN_HEIGHT)
        welcomeView?.isPagingEnabled = true
        welcomeView?.bounces = false
        welcomeView?.showsHorizontalScrollIndicator = false
        welcomeView?.showsVerticalScrollIndicator = false
        self.view.addSubview(welcomeView!)
        
        unowned let WS = self
        welcomeView?.snp.makeConstraints({ (make) -> Void in
            make.top.left.bottom.equalTo(WS.view).offset(0)
            make.width.equalTo(AMPConstants.AMP_SCREEN_WIDTH)
        })
        
        for i in 1...3 {

            let imageView = UIImageView()
            imageView.image = UIImage(named: String(format: "welcome_%d",i))
            welcomeView?.addSubview(imageView)
            imageView.snp.makeConstraints({ (make) -> Void in
                make.top.equalTo(welcomeView!).offset(0)
                make.left.equalTo(welcomeView!).offset(CGFloat(i - 1)*AMPConstants.AMP_SCREEN_WIDTH)
                make.width.equalTo(AMPConstants.AMP_SCREEN_WIDTH)
                make.height.equalTo(AMPConstants.AMP_SCREEN_HEIGHT)
            })
            
            if i == 3 {
                let btn = UIButton(type: UIButtonType.custom)
                btn.backgroundColor = AMPConstants.AMPMainColor
                btn.layer.cornerRadius = 20;
                btn.layer.masksToBounds = true
                btn.setTitle("立即体验", for: UIControlState())
                btn.setTitleColor(UIColor.white, for: UIControlState.normal)
                btn.setTitleColor(UIColor.black, for: UIControlState.highlighted)
                welcomeView?.addSubview(btn)
                
                btn.addTarget(self, action: #selector(toMian), for: UIControlEvents.touchUpInside)
                
                btn.snp.makeConstraints({ (make) -> Void in
                    make.bottom.equalTo(welcomeView!).offset(AMPConstants.AMP_SCREEN_HEIGHT - 80)
                    make.left.equalTo(welcomeView!).offset(CGFloat(i-1)*AMPConstants.AMP_SCREEN_WIDTH + AMPConstants.AMP_SCREEN_WIDTH*0.5 - 90)
                    make.width.equalTo(180)
                    make.height.equalTo(40)
                })
            }
        }
    }
    
    // MARK: Events
    func toMian(){
        
        UIApplication.shared.isStatusBarHidden = false
        UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: true)
        UIApplication.shared.delegate?.window!!.rootViewController = MainViewController()
    }

}
